rm(list=ls()) #clear

library(summarytools)
library(expss)
library(dplyr)
library(psych)

std01<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
} 
#recode vars 0 to 1


###merging qualtrics and demographics data###

qual<- read.csv("Study 4 - Bovitz/Wave 1/Bovitz_Qual_S4W1.csv")
demo<- read.csv("Study 4 - Bovitz/Wave 1/Bovitz_Demo_S4W1.csv")

dat<-merge(qual, demo, by="RESPONDENT_ID", all = "TRUE") 

demoexport<- dat[c("PID", colnames(demo))]
demoexport<- demoexport[!(duplicated(demoexport$PID) | duplicated(demoexport$PID, fromLast = TRUE)), ]

  # demographics for all respondents sent to respective folders
save(demoexport, file = "Study 4 - Bovitz/Wave 2/demoexport.RData")
save(demoexport, file = "Study 4 - Bovitz/Wave 3/demoexport.RData")

  # removing disqualified and Rs from other group
dat<- subset(dat, dat$Finished==1)
dat<- subset(dat, dat$Consent==1)
dat<- subset(dat, dat$atnchk1=="1,2")
dat<- dat[!(duplicated(dat$PID) | duplicated(dat$PID, fromLast = TRUE)), ]

dat$pid7<- ifelse(dat$pidcat==1 & dat$demstr==1, 1,
                  ifelse(dat$pidcat==1 & dat$demstr==2, 2,
                         ifelse(dat$pidcat==2 & dat$demstr==1, 7,
                                ifelse(dat$pidcat==2 & dat$demstr==2, 6, dat$lean))))
dat$pid0to1<- std01(dat$pid7)
freq(dat$pid7)

#party ID
dat$pid01<- ifelse(dat$pid7<4, 0, 1)
#0 = Dem and 1 = Rep

################################### DEMOGRAPHIC VARS ##################################
dat$white<- ifelse(dat$ethnicity==1, 1, 0)
dat$black<- ifelse(dat$ethnicity==2, 1, 0)
dat$asian<- ifelse(dat$ethnicity==3, 1, 0)
dat$male<- ifelse(dat$gender==1, 1, 0)
dat$hisplat<- ifelse(dat$hispanic==1, 1, 0)
dat$inc<- dat$income
dat$ba<- ifelse(dat$education > 5, 1, 0)

freq(dat$white)
freq(dat$black)
freq(dat$asian)
freq(dat$male)
freq(dat$hisplat)
freq(dat$ba)
mean(dat$age)

dat<- subset(dat, dat$pid7 != 4)
#everyone but pure independents
freq(dat$pid01)
dat<- subset(dat, dat$atnchk2==5)
#2nd attention check that was halfway through the survey
################################### normal vars #######################################

#aggregate aps and subscale items for both 20 and 9 versions
dat$otot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8)))
dat$atot20<-rowMeans(with(dat, cbind(a1, a2, a3, a4, a5, a6)))
dat$mtot20<-rowMeans(with(dat, cbind(m1, m2, m3, m4, m5, m6)))
dat$apstot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8,
                                                a1, a2, a3, a4, a5, a6,
                                                m1, m2, m3, m4, m5, m6)))

dat$otot9<-rowMeans(with(dat, cbind(o1, o6, o8)))
dat$atot9<-rowMeans(with(dat, cbind(a1, a2, a4)))
dat$mtot9<-rowMeans(with(dat, cbind(m2, m4, m6)))
dat$apstot9<-rowMeans(with(dat, cbind(o1, o6, o8,
                                               a1, a2, a4, 
                                               m2, m4, m6)))
descr(dat$apstot9)
descr(dat$otot9)
descr(dat$atot9)
descr(dat$mtot9)

# party ID extremity (RELABELED)
# 0, 1, 2 coding:
dat$pidex<- ifelse(dat$pid7==1 | dat$pid7==7, 2, 
                   ifelse(dat$pid7==2 | dat$pid7==6, 1,
                          ifelse(dat$pid7==3 | dat$pid7==5, 0, NA)))
# 0, 0.5, 1 coding:
dat$rpidex<-std01(dat$pidex)
# make 0, 1, 2 coding a factor variable
dat$fpidex<-as.factor(dat$pidex)

#ideology
dat$idex<- abs(dat$political_ideology-4)
dat$ridex<- std01(dat$idex)
dat$fidex<- as.factor(dat$ridex)

#warmth bias (higher score means greater gap in ft)
dat$ftinparty<- ifelse(dat$pid01==0, dat$ft_2, dat$ft_1)
dat$ftoutparty<- ifelse(dat$pid01==0, dat$ft_1, dat$ft_2)
dat$wbias<- (dat$ftinparty-dat$ftoutparty)

#Huddy et. al. partisan social identity (higher means stronger partisan identity)
dat$idtot<-rowMeans(with(dat, cbind(hid1, hid2, hid3, hid4)))
psych::alpha(with(dat, cbind(hid1, hid2, hid3, hid4))) #.85

#political knowledge (higher means more political knowledge)
dat$pk1<- ifelse(dat$pk1==4, 1, 0)
dat$pk2<- ifelse(dat$pk2==2, 1, 0)
dat$pk3<- ifelse(dat$pk3==1, 1, 0)
dat$pk4<- ifelse(dat$pk4==3, 1, 0)

dat$pktot<-rowMeans(with(dat, cbind(pk1, pk2, pk3, pk4)))
psych::alpha(with(dat, cbind(pk1, pk2, pk3, pk4))) #.48
#not super consistent

#political violence (higher means more violence)
dat$pv2<- dplyr::recode(dat$pv2, "1"=5, "2"=4, "3"=3, "4"=2, "5"=1)

dat$pvtot<-rowMeans(with(dat, cbind(pv1, pv2, pv5, pv6, pv7)))
psych::alpha(with(dat, cbind(pv1, pv2, pv5, pv6, pv7))) #.81

#recode all measures to 0-1
dat<- dat %>%
  mutate_at(c("otot20", "atot20", "mtot20", "apstot20",
              "otot9", "atot9", "mtot9", "apstot9",
              "wbias", "ftinparty", "ftoutparty",
              "idtot", "pktot", "pvtot",
              "age", "inc"),
            funs(std01(.)))

save(dat, file = "Study 4 - Bovitz/Wave 1/dataS4W1.RData")

dat4<- dat[c("o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8",
              "a1", "a2", "a3", "a4", "a5", "a6",
              "m1", "m2", "m3", "m4", "m5", "m6",
              "otot20", "atot20", "mtot20", "apstot20",
              "otot9", "atot9", "mtot9", "apstot9",
              "wbias", "ftinparty", "ftoutparty",
              "idtot", "pktot", "pvtot",
              "pid7", "pid01", "rpidex", "fpidex", "ridex", "fidex",
              "white", "male", "hisplat", "age", "inc", "ba")]

save(dat4, file = "Main Analyses/dataS4.RData")

datT1<- dat[c("Finished", "o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8",
                   "a1", "a2", "a3", "a4", "a5", "a6",
                   "m1", "m2", "m3", "m4", "m5", "m6",
                   "otot20", "atot20", "mtot20", "apstot20",
                   "otot9", "atot9", "mtot9", "apstot9",
                   "wbias", "ftinparty", "ftoutparty",
                   "idtot", "pktot", "pvtot", "pid7", "pid01", "PID",
                   "white", "black", "asian", "male", "hisplat", "age", "inc", "ba")]
save(datT1, file = "Study 4 - Bovitz/TS/dataT1.RData")


